<?php

namespace app\admin\controller;

use app\admin\service\Auth;
use app\common\gateway\data\JsonData;
use DI\Annotation\Inject;
use GatewayWorker\Lib\Gateway;
use PragmaRX\Random\Random;
use support\Request;
use app\admin\model\Admin as Model;
use support\util\TreeArray;

class Admin extends BaseController
{
    /**
     * @var Auth
     * @Inject()
     */
    protected $auth = null;

    protected $preViewName = 'admin';

    protected $model = Model::class;

    protected function store(Request $request)
    {
        $form = $request->post('form');
        $random = new Random();
        if (isset($form['id'])) {
            $row = Model::query()->where('id', $form['id'])->first();
            if (!$row) {
                $this->error('未找到!');
            }
            if (isset($form['password']) && !empty($form['password'])) {
                $row->salt = $random->alpha()->size(6)->get();
                $row->password = Auth::getEncryptPassword($form['password'], $row->salt);
            } else {
                unset($form['password']);
            }
        }else {
            $row = Model::query()->where('username', $form['username'])->first();
            if ($row) {
                $this->error("已存在该账号!");
            }
            $row = new \app\admin\model\Admin();
            $row->salt = $random->alpha()->size(6)->get();
            $row->password = Auth::getEncryptPassword($form['password'], $row->salt);
            $row->logintime = date('Y-m-d H:i:s');
        }
        $row->fill($form);
        $row->save();
        $row->roles()->sync($form['roleids']);
        $this->success("操作成功!");
    }

    public function sendmsg(Request $request)
    {
        $id = $request->post('id');
        $msg = $request->post('msg');
        $user = $this->auth->getUserModel();
        $toUser = Model::query()->where('id', $id)->first();
        if (!$toUser) {
            $this->error("未找到");
        }
        if (!Gateway::isUidOnline($id)) {
            $this->error("用户不在线!");
        }
        $data = new JsonData('alert_msg', [
            'code' => 1,
            'msg' => $user->nickname. ":" . $msg
        ]);
        /** @var Model $toUser */
        $toUser->sendMsg($data);
        $this->success("操作成功!");
    }

    public function logout(Request $request)
    {
        $id = $request->post('id');
        $clientLists = Gateway::getClientIdByUid($id);
        foreach ($clientLists as $client_id) {
            Gateway::closeClient($client_id);
        }
        $this->success("操作成功!");
    }

    public function role(Request $request)
    {
        $roleIds = $this->auth->getRoleList()->pluck('id');
        // 获取当前登录的管理员角色列表
        $roleList = \app\admin\model\Role::get();
        $roleArr = $roleList->toArray();
        foreach ($roleArr as &$role) {
            $role['checked'] = in_array($role['id'],$roleIds->toArray());
        }
        $tree = TreeArray::instance();
        $tree->init($roleArr);
        $this->success("", $tree->getTreeArray(0));
    }
}
